class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


def rangeSumBST(root, low, high):
    if root == None:
        return 0
    left_sum = rangeSumBST(root.left, low, high)
    right_sum = rangeSumBST(root.right, low, high)
    res = left_sum + right_sum
    if root.val >= low and root.val <= high:
        res += root.val
    return res


root = TreeNode(10)
root.left = TreeNode(5)
root.right = TreeNode(15)
root.right.right = TreeNode(18)
root.left.left = TreeNode(3)
root.left.right = TreeNode(7)
print(rangeSumBST(root, 7, 15))  # 32
